/**
 * 
 * The Element class is an abstract class from which Brick, Player and Bomb all inherit.
 * This so-called base class holds the row, column and coordinates for each game element.
 * The class also holds a couple a utility functions.
 * 
 * Author: Matthias Van Eeghem <matthias@van-eeghem.com>
 * All rights reserved.
 * 
 */

package com.bomberman.core;

import java.util.ArrayList;

public abstract class Element 
{
	/**
	 * List of all element (bricks, explosions, other players, bombs)
	 */
	static ArrayList<Element> elementList = new ArrayList<Element>();	
	/**
	 * Represents the row the element is located on
	 */
	private int row;
	/**
	 * Represents the column the element is located on
	 */
	private int column;
	/**
	 * Represents the minimum x coordinate for the element
	 * this is the coordinate of the left hand side of the element.
	 */
	protected int minimumXCoordinate;
	/**
	 * Represents the maximum y coordinate for the element
	 * this is the coordinate of the top of the element
	 * Is actually the lowest y coordinate in value. (The y axis is flipped)
	 */
	protected int maximumYCoordinate;
	/**
	 * Represents the maximum x coordinate for the element
	 * this is the coordinate of the right hand side of the element.
	 */
	protected int maximumXCoordinate;
	/**
	 * Represents the minimum y coordinate for the element
	 * this is the coordinate of the bottom of the element
	 * Is actually the lowest y coordinate in value. (The y axis is flipped)
	 */
	protected int minimumYCoordinate;

	/**
	 * Possible element types
	 */
	public enum ElementType
	{
		TYPE_PLAYER,
		TYPE_BRICK,
		TYPE_BOMB,
		TYPE_EXPLOSION
	}
	
	/**
	 * Holds the element type
	 */
	private ElementType elementType;
	
	/**
	 * The Element constructor, initialises all variables.
	 * 
	 * @param row The row the element should be located on
	 * @param column The column the element should be located on
	 */
	public Element(int row, int column, ElementType elementType)
	{
		this.elementType = elementType;
		
		// Save the row and column
		this.row = row;
		this.column = column;
	
		// Calculate its coordinates from the column and row
		this.maximumYCoordinate = (Game.FIRSTBRICK_MAX_Y_COORDINATE + (Game.BRICK_WIDTH * row) - 1);
		this.minimumYCoordinate = (Game.FIRSTBRICK_MAX_Y_COORDINATE + (Game.BRICK_WIDTH * (row + 1)) - 1);
		this.maximumXCoordinate = (Game.FIRSTBRICK_MIN_X_COORDINATE + (Game.BRICK_WIDTH * (column + 1)));
		this.minimumXCoordinate = (Game.FIRSTBRICK_MIN_X_COORDINATE + (Game.BRICK_WIDTH * column));
		
		elementList.add(this);
	}
	
	/**
	 * Returns the row the element is located on.
	 * 
	 * @return The row
	 */
	public int getRow()
	{
		return row;
	}
	
	/**
	 * Returns the column the element is located on.
	 * 
	 * @return The column
	 */
	public int getColumn()
	{
		return column;
	}
	
	/**
	 * Returns the element's minimum x coordinate
	 * 
	 * @return The minimum x coordinate
	 */
	public int getMinimumXCoordinate()
	{
		return minimumXCoordinate;
	}
	
	/**
	 * Returns the element's minimum y coordinate
	 * 
	 * @return The minimum y coordinate
	 */
	public int getMinimumYCoordinate()
	{
		return minimumYCoordinate;
	}
	
	/**
	 * Returns the element's maximum x coordinate
	 * 
	 * @return The maximum x coordinate
	 */	
	public int getMaximumXCoordinate()
	{
		return maximumXCoordinate;
	}
	
	/**
	 * Returns the element's maximum y coordinate
	 * 
	 * @return The maximum y coordinate
	 */
	public int getMaximumYCoordinate()
	{
		return maximumYCoordinate;
	}
	
	/**
	 * Returns the element type
	 * 
	 * @return The element type
	 */
	public ElementType getElementType()
	{
		return elementType;
	}
	
	/**
	 * Returns a list of all elements created
	 * 
	 * @return The element list
	 */
	public static ArrayList<Element> getElementList()
	{
		return elementList;
	}
	
	/**
	 * This method returns true if a brick is built on the location marked by
	 * the row and column, false otherwise. 
	 * 
	 * @param row The row to check for
	 * @param column The column to check for
	 * @return true if the brick was built, false otherwise
	 */
	public static boolean isAnyElementAtPosition(int row, int column)
	{
		// Loop through all the elements
		for(Element element : elementList)
		{
			// Ignore players
			if(element.getElementType() == ElementType.TYPE_PLAYER)
				continue;
			
			// Ignore explosions
			if(element.getElementType() == ElementType.TYPE_EXPLOSION)
				continue;
			
			if(element.getRow() == row && element.getColumn() == column)
				return true;
		}
		
		return false;
	}
	
	/**
	 * Removes an object from the element list.
	 * 
	 * @param object The object to remove
	 */
	public static void remove(Object object)
	{
		elementList.remove(object);
	}
}
